//
//  ZSNLuoDataViewController.swift
//  AgoraBroadCastNewSwiftZhaosn
//
//  Created by zhaosuning on 2022/4/15.
//

import UIKit
import AgoraRtcKit

class ZSNLuoDataViewController: UIViewController {
    
    

    var agoraKit: AgoraRtcEngineKit!
    var role: AgoraClientRole = .broadcaster
    
    var localView: UIView!
    var remoteView: UIView!
    
    var btnEarMonitorNull: UIButton!
    var btnEarMonitorNone: UIButton!
    var btnbuiltInAudioFilters: UIButton!
    var btnnoiseSuppression: UIButton!
    var btnCloseEra: UIButton!
    
    var intAudioFilters: Int = 0
    
    var  myTimer: Timer!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = UIColor.white
        self.title = "耳返960Stereo"
        intAudioFilters = 0
        
        let btnJoinChannel = UIButton.init(frame: CGRect(x: 240, y: 410, width: 100, height: 40))
        btnJoinChannel.backgroundColor = UIColor.green
        btnJoinChannel.setTitle("进入频道", for: .normal)
        btnJoinChannel.layer.cornerRadius = 20
        btnJoinChannel.titleLabel?.textAlignment = .center
        btnJoinChannel.layer.masksToBounds = true
        btnJoinChannel.titleLabel?.font = UIFont.systemFont(ofSize: 17)
        btnJoinChannel.setTitleColor(UIColor(red: 50/255, green: 71/255, blue: 1, alpha: 1), for: UIControl.State.normal)
        btnJoinChannel.addTarget(self, action: #selector(btnJoinChannelClick), for: UIControl.Event.touchUpInside)
        self.view.addSubview(btnJoinChannel)
        
        let btnLeaveChannel = UIButton.init(frame: CGRect(x: 110, y: 410, width: 100, height: 40))
        btnLeaveChannel.backgroundColor = UIColor.green
        btnLeaveChannel.setTitle("离开频道", for: .normal)
        btnLeaveChannel.layer.cornerRadius = 20
        btnLeaveChannel.titleLabel?.textAlignment = .center
        btnLeaveChannel.layer.masksToBounds = true
        btnLeaveChannel.titleLabel?.font = UIFont.systemFont(ofSize: 17)
        btnLeaveChannel.setTitleColor(UIColor(red: 50/255, green: 71/255, blue: 1, alpha: 1), for: UIControl.State.normal)
        btnLeaveChannel.addTarget(self, action: #selector(btnLeaveChannelClick), for: UIControl.Event.touchUpInside)
        self.view.addSubview(btnLeaveChannel)
        
        let btnDestroy = UIButton.init(frame: CGRect(x: 5, y: 410, width: 70, height: 40))
        btnDestroy.backgroundColor = UIColor.green
        btnDestroy.setTitle("销毁", for: .normal)
        btnDestroy.layer.cornerRadius = 20
        btnDestroy.titleLabel?.textAlignment = .center
        btnDestroy.layer.masksToBounds = true
        btnDestroy.titleLabel?.font = UIFont.systemFont(ofSize: 17)
        btnDestroy.setTitleColor(UIColor(red: 50/255, green: 71/255, blue: 1, alpha: 1), for: UIControl.State.normal)
        btnDestroy.addTarget(self, action: #selector(btnDestroyClick), for: UIControl.Event.touchUpInside)
        self.view.addSubview(btnDestroy)
        
        btnCloseEra = UIButton.init(frame: CGRect(x: 1, y: 100, width: 373, height: 40))
        btnCloseEra.backgroundColor = UIColor.red
        btnCloseEra.setTitle("默认关闭耳返", for: .normal)
        btnCloseEra.layer.cornerRadius = 20
        btnCloseEra.titleLabel?.textAlignment = .center
        btnCloseEra.layer.masksToBounds = true
        btnCloseEra.titleLabel?.font = UIFont.systemFont(ofSize: 12)
        btnCloseEra.setTitleColor(UIColor(red: 50/255, green: 71/255, blue: 1, alpha: 1), for: UIControl.State.normal)
        btnCloseEra.addTarget(self, action: #selector(btnCloseEraClick), for: UIControl.Event.touchUpInside)
        self.view.addSubview(btnCloseEra)
        
        
        btnEarMonitorNull = UIButton.init(frame: CGRect(x: 1, y: 160, width: 373, height: 40))
        btnEarMonitorNull.backgroundColor = UIColor.lightGray
        btnEarMonitorNull.setTitle("enableInEarMonitoring(inEarMonitoring: true)", for: .normal)
        btnEarMonitorNull.layer.cornerRadius = 20
        btnEarMonitorNull.titleLabel?.textAlignment = .center
        btnEarMonitorNull.layer.masksToBounds = true
        btnEarMonitorNull.titleLabel?.font = UIFont.systemFont(ofSize: 12)
        btnEarMonitorNull.setTitleColor(UIColor(red: 50/255, green: 71/255, blue: 1, alpha: 1), for: UIControl.State.normal)
        btnEarMonitorNull.addTarget(self, action: #selector(btnEarMonitorNullClick), for: UIControl.Event.touchUpInside)
        self.view.addSubview(btnEarMonitorNull)
        
        
        btnEarMonitorNone = UIButton.init(frame: CGRect(x: 1, y: 220, width: 373, height: 40))
        btnEarMonitorNone.backgroundColor = UIColor.lightGray
        btnEarMonitorNone.setTitle("enableInEarMonitoring(true, AudioFilters: .none)", for: .normal)
        btnEarMonitorNone.layer.cornerRadius = 20
        btnEarMonitorNone.titleLabel?.textAlignment = .center
        btnEarMonitorNone.layer.masksToBounds = true
        btnEarMonitorNone.titleLabel?.font = UIFont.systemFont(ofSize: 12)
        btnEarMonitorNone.setTitleColor(UIColor(red: 50/255, green: 71/255, blue: 1, alpha: 1), for: UIControl.State.normal)
        btnEarMonitorNone.addTarget(self, action: #selector(btnEarMonitorNoneClick), for: UIControl.Event.touchUpInside)
        self.view.addSubview(btnEarMonitorNone)
        
        
        btnbuiltInAudioFilters = UIButton.init(frame: CGRect(x: 1, y: 280, width: 373, height: 40))
        btnbuiltInAudioFilters.backgroundColor = UIColor.lightGray
        btnbuiltInAudioFilters.setTitle("enableInEarMonitoring(true, AudioFilters: .builtInAudioFilters)", for: .normal)
        btnbuiltInAudioFilters.layer.cornerRadius = 20
        btnbuiltInAudioFilters.titleLabel?.textAlignment = .center
        btnbuiltInAudioFilters.layer.masksToBounds = true
        btnbuiltInAudioFilters.titleLabel?.font = UIFont.systemFont(ofSize: 12)
        btnbuiltInAudioFilters.setTitleColor(UIColor(red: 50/255, green: 71/255, blue: 1, alpha: 1), for: UIControl.State.normal)
        btnbuiltInAudioFilters.addTarget(self, action: #selector(btnbuiltInAudioFiltersClick), for: UIControl.Event.touchUpInside)
        self.view.addSubview(btnbuiltInAudioFilters)
        
        btnnoiseSuppression = UIButton.init(frame: CGRect(x: 1, y: 340, width: 373, height: 40))
        btnnoiseSuppression.backgroundColor = UIColor.lightGray
        btnnoiseSuppression.setTitle("enableInEarMonitoring(true, AudioFilters: .noiseSuppression)", for: .normal)
        btnnoiseSuppression.layer.cornerRadius = 20
        btnnoiseSuppression.titleLabel?.textAlignment = .center
        btnnoiseSuppression.layer.masksToBounds = true
        btnnoiseSuppression.titleLabel?.font = UIFont.systemFont(ofSize: 12)
        btnnoiseSuppression.setTitleColor(UIColor(red: 50/255, green: 71/255, blue: 1, alpha: 1), for: UIControl.State.normal)
        btnnoiseSuppression.addTarget(self, action: #selector(btnnoiseSuppressionClick), for: UIControl.Event.touchUpInside)
        self.view.addSubview(btnnoiseSuppression)
        
        
        localView = UIView.init()
        localView.backgroundColor = UIColor.yellow
        localView.frame = CGRect(x: 2, y: 470, width: 150, height: 150)
        self.view.addSubview(localView)
        
        remoteView = UIView.init()
        remoteView.frame = CGRect(x: 165, y: 470, width: 200, height: 200)
        remoteView.backgroundColor = UIColor.blue
        self.view.addSubview(remoteView)
        
        let btnStartTimer = UIButton.init(frame: CGRect(x: 5, y: 520, width: 120, height: 40))
        btnStartTimer.backgroundColor = UIColor.green
        btnStartTimer.setTitle("启动定时器", for: .normal)
        btnStartTimer.layer.cornerRadius = 20.0
        btnStartTimer.titleLabel?.textAlignment = .center
        btnStartTimer.layer.masksToBounds = true
        btnStartTimer.titleLabel?.font = UIFont.systemFont(ofSize: 20)
        btnStartTimer.setTitleColor(UIColor(red: 50/255, green: 71/255, blue: 1, alpha: 1), for: UIControl.State.normal)
        btnStartTimer.addTarget(self, action: #selector(btnStartTimerClick), for: UIControl.Event.touchUpInside)
        self.view.addSubview(btnStartTimer)
        
        let btnStopTimer = UIButton.init(frame: CGRect(x: 5, y: 460, width: 120, height: 40))
        btnStopTimer.backgroundColor = UIColor.green
        btnStopTimer.setTitle("销毁定时器", for: .normal)
        btnStopTimer.layer.cornerRadius = 20.0
        btnStopTimer.titleLabel?.textAlignment = .center
        btnStopTimer.layer.masksToBounds = true
        btnStopTimer.titleLabel?.font = UIFont.systemFont(ofSize: 20)
        btnStopTimer.setTitleColor(UIColor(red: 50/255, green: 71/255, blue: 1, alpha: 1), for: UIControl.State.normal)
        btnStopTimer.addTarget(self, action: #selector(btnStopTimerClick), for: UIControl.Event.touchUpInside)
        self.view.addSubview(btnStopTimer)

        
        let btnRoleAudience = UIButton.init(frame: CGRect(x: 135, y: 520, width: 120, height: 40))
        btnRoleAudience.backgroundColor = UIColor.green
        btnRoleAudience.setTitle("变为观众", for: .normal)
        btnRoleAudience.layer.cornerRadius = 20.0
        btnRoleAudience.titleLabel?.textAlignment = .center
        btnRoleAudience.layer.masksToBounds = true
        btnRoleAudience.titleLabel?.font = UIFont.systemFont(ofSize: 20)
        btnRoleAudience.setTitleColor(UIColor(red: 50/255, green: 71/255, blue: 1, alpha: 1), for: UIControl.State.normal)
        btnRoleAudience.addTarget(self, action: #selector(btnRoleAudienceClick), for: UIControl.Event.touchUpInside)
        self.view.addSubview(btnRoleAudience)
        
        let btnBroadcaster = UIButton.init(frame: CGRect(x: 135, y: 460, width: 120, height: 40))
        btnBroadcaster.backgroundColor = UIColor.green
        btnBroadcaster.setTitle("变为主播", for: .normal)
        btnBroadcaster.layer.cornerRadius = 20.0
        btnBroadcaster.titleLabel?.textAlignment = .center
        btnBroadcaster.layer.masksToBounds = true
        btnBroadcaster.titleLabel?.font = UIFont.systemFont(ofSize: 20)
        btnBroadcaster.setTitleColor(UIColor(red: 50/255, green: 71/255, blue: 1, alpha: 1), for: UIControl.State.normal)
        btnBroadcaster.addTarget(self, action: #selector(btnBroadcasterClick), for: UIControl.Event.touchUpInside)
        self.view.addSubview(btnBroadcaster)
        
        let btnPlayMusic = UIButton.init(frame: CGRect(x: 265, y: 460, width: 120, height: 40))
        btnPlayMusic.backgroundColor = UIColor.green
        btnPlayMusic.setTitle("播放Music", for: .normal)
        btnPlayMusic.layer.cornerRadius = 20.0
        btnPlayMusic.titleLabel?.textAlignment = .center
        btnPlayMusic.layer.masksToBounds = true
        btnPlayMusic.titleLabel?.font = UIFont.systemFont(ofSize: 20)
        btnPlayMusic.setTitleColor(UIColor(red: 50/255, green: 71/255, blue: 1, alpha: 1), for: UIControl.State.normal)
        btnPlayMusic.addTarget(self, action: #selector(btnPlayMusicClick), for: UIControl.Event.touchUpInside)
        self.view.addSubview(btnPlayMusic)
        
        let btnStopMusic = UIButton.init(frame: CGRect(x: 265, y: 520, width: 120, height: 40))
        btnStopMusic.backgroundColor = UIColor.green
        btnStopMusic.setTitle("停止Music", for: .normal)
        btnStopMusic.layer.cornerRadius = 20.0
        btnStopMusic.titleLabel?.textAlignment = .center
        btnStopMusic.layer.masksToBounds = true
        btnStopMusic.titleLabel?.font = UIFont.systemFont(ofSize: 20)
        btnStopMusic.setTitleColor(UIColor(red: 50/255, green: 71/255, blue: 1, alpha: 1), for: UIControl.State.normal)
        btnStopMusic.addTarget(self, action: #selector(btnStopMusicClick), for: UIControl.Event.touchUpInside)
        self.view.addSubview(btnStopMusic)
        
        let btnLocalAudioNo = UIButton.init(frame: CGRect(x: 5, y: 575, width: 300, height: 35))
        btnLocalAudioNo.backgroundColor = UIColor.green
        btnLocalAudioNo.setTitle("enableLocalAudio:False", for: .normal)
        btnLocalAudioNo.layer.cornerRadius = 17.0
        btnLocalAudioNo.titleLabel?.textAlignment = .center
        btnLocalAudioNo.layer.masksToBounds = true
        btnLocalAudioNo.titleLabel?.font = UIFont.systemFont(ofSize: 20)
        btnLocalAudioNo.setTitleColor(UIColor(red: 50/255, green: 71/255, blue: 1, alpha: 1), for: UIControl.State.normal)
        btnLocalAudioNo.addTarget(self, action: #selector(btnLocalAudioNoClick), for: UIControl.Event.touchUpInside)
        self.view.addSubview(btnLocalAudioNo)
        
        let btnLocalAudioYes = UIButton.init(frame: CGRect(x: 5, y: 620, width: 300, height: 35))
        btnLocalAudioYes.backgroundColor = UIColor.green
        btnLocalAudioYes.setTitle("enableLocalAudio:True", for: .normal)
        btnLocalAudioYes.layer.cornerRadius = 17.0
        btnLocalAudioYes.titleLabel?.textAlignment = .center
        btnLocalAudioYes.layer.masksToBounds = true
        btnLocalAudioYes.titleLabel?.font = UIFont.systemFont(ofSize: 20)
        btnLocalAudioYes.setTitleColor(UIColor(red: 50/255, green: 71/255, blue: 1, alpha: 1), for: UIControl.State.normal)
        btnLocalAudioYes.addTarget(self, action: #selector(btnLocalAudioYesClick), for: UIControl.Event.touchUpInside)
        self.view.addSubview(btnLocalAudioYes)
        
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        btnLeaveChannelClick()
    }
    
    @objc func btnPlayMusicClick() {
        let strurl: String = Bundle.main.path(forResource: "jiandanai", ofType: "mp3")!
        agoraKit.startAudioMixing(strurl, loopback: false, replace: false, cycle: 1, startPos: 0)
    }
    
    @objc func btnStopMusicClick() {
        agoraKit.stopAudioMixing()
    }
    
    @objc func btnLocalAudioNoClick() {
        agoraKit.enableLocalAudio(false)
    }
    
    @objc func btnLocalAudioYesClick() {
        agoraKit.enableLocalAudio(true)
    }
    
    @objc func btnStartTimerClick() {
        myTimer = Timer.scheduledTimer(timeInterval: 1.0/15.0,
                                       target:self,selector:#selector(ontimerfunc),
                                       userInfo:nil,repeats:true)
        myTimer.fire()
    }
    
    
    @objc func btnStopTimerClick() {
        myTimer.invalidate()
        myTimer = nil
        NSLog("打印了 定时器 销毁")
    }
    
    @objc func ontimerfunc() {
        let i = agoraKit.getAudioMixingCurrentPosition()
        NSLog("打印了 getAudioMixingCurrentPosition \(i)")
    }
    
    
    @objc func btnCloseEraClick() {
        intAudioFilters = 0
        btnEarMonitorNull.backgroundColor = UIColor.lightGray
        btnEarMonitorNone.backgroundColor = UIColor.lightGray
        btnbuiltInAudioFilters.backgroundColor = UIColor.lightGray
        btnnoiseSuppression.backgroundColor = UIColor.lightGray
        btnCloseEra.backgroundColor = UIColor.red
    }
    
    
    @objc func btnEarMonitorNullClick() {
        intAudioFilters = 1
        btnEarMonitorNull.backgroundColor = UIColor.red
        btnEarMonitorNone.backgroundColor = UIColor.lightGray
        btnbuiltInAudioFilters.backgroundColor = UIColor.lightGray
        btnnoiseSuppression.backgroundColor = UIColor.lightGray
        btnCloseEra.backgroundColor = UIColor.lightGray
    }
    
    @objc func btnEarMonitorNoneClick() {
        intAudioFilters = 2
        btnEarMonitorNull.backgroundColor = UIColor.lightGray
        btnEarMonitorNone.backgroundColor = UIColor.red
        btnbuiltInAudioFilters.backgroundColor = UIColor.lightGray
        btnnoiseSuppression.backgroundColor = UIColor.lightGray
        btnCloseEra.backgroundColor = UIColor.lightGray
    }
    
    @objc func btnbuiltInAudioFiltersClick() {
        intAudioFilters = 3
        btnEarMonitorNull.backgroundColor = UIColor.lightGray
        btnEarMonitorNone.backgroundColor = UIColor.lightGray
        btnbuiltInAudioFilters.backgroundColor = UIColor.red
        btnnoiseSuppression.backgroundColor = UIColor.lightGray
        btnCloseEra.backgroundColor = UIColor.lightGray
    }
    
    @objc func btnnoiseSuppressionClick() {
        intAudioFilters = 4
        btnEarMonitorNull.backgroundColor = UIColor.lightGray
        btnEarMonitorNone.backgroundColor = UIColor.lightGray
        btnbuiltInAudioFilters.backgroundColor = UIColor.lightGray
        btnnoiseSuppression.backgroundColor = UIColor.red
        btnCloseEra.backgroundColor = UIColor.lightGray
    }
    
    @objc func btnRoleAudienceClick() {
        agoraKit.setClientRole(.audience)
    }
    
    @objc func btnBroadcasterClick() {
        agoraKit.setClientRole(.broadcaster)
    }
    

    @objc func btnDestroyClick() {
        AgoraRtcEngineKit.destroy()
    }
    
    @objc func btnJoinChannelClick() {
        joinChannella()
    }
    
    @objc func btnLeaveChannelClick() {
        if (agoraKit != nil) {
            agoraKit.leaveChannel { (stats) -> Void in
                NSLog("打印了 离开频道")
            }
        }
    }
    
    func joinChannella() {
        let config = AgoraRtcEngineConfig()
        config.appId = ZSNComDef.AppId
        config.channelProfile = .liveBroadcasting
        agoraKit = AgoraRtcEngineKit.sharedEngine(with: config, delegate: self)
        let channelName = ZSNComDef.ChannelName
        
        //let options = AgoraClientRoleOptions()
        //options.audienceLatencyLevel = .ultraLowLatency
        //options.audienceLatencyLevel = .lowLatency
        //agoraKit.setClientRole(.audience, options: options)
        
        //agoraKit.setClientRole(.broadcaster, options: nil)
        
        agoraKit.setClientRole(.broadcaster)
        agoraKit.setAudioProfile(.musicStandardStereo, scenario: .gameStreaming)
        agoraKit.enableAudio()
        agoraKit.setVideoFrameDelegate(self)
        agoraKit.enableVideo()
        
        agoraKit.setVideoEncoderConfiguration(AgoraVideoEncoderConfiguration(size: AgoraVideoDimension640x360,
                                                                             frameRate: .fps30,
                                                                             bitrate: AgoraVideoBitrateStandard,
                                                                             orientationMode: .adaptative, mirrorMode: .auto))
        
        
        // setup raw media data observers
        agoraKit.setAudioFrameDelegate(self)
        
        agoraKit.setRecordingAudioFrameParametersWithSampleRate(44100, channel: 2, mode: .readWrite, samplesPerCall: 882)
        agoraKit.setMixedAudioFrameParametersWithSampleRate(44100, channel: 2, samplesPerCall: 882)
        agoraKit.setPlaybackAudioFrameParametersWithSampleRate(44100, channel: 2, mode: .readWrite, samplesPerCall: 882)
        agoraKit.setEarMonitoringAudioFrameParametersWithSampleRate(44100, channel: 2, mode: .readWrite, samplesPerCall: 882)
        
        //耳返
        //开启耳返 不带AgoraEarMonitoringFilterType
        
        //agoraKit.enable(inEarMonitoring: true)
        
        
        //1: Do not add an audio filter to the in-ear monitor.
        //AgoraEarMonitoringFilterNone = (1<<0),
      
        //2: Add an audio filter to the in-ear monitor.
        //AgoraEarMonitoringFilterBuiltInAudioFilters = (1<<1),
            
        // Enable noise suppression to the in-ear monitor.
        //AgoraEarMonitoringFilterNoiseSuppression = (1<<2)
        
        //开启耳返 带AgoraEarMonitoringFilterType
        //不在耳返中添加 audio filter
        //agoraKit.enable(inEarMonitoring: true, includeAudioFilters: .none)
        
        //在耳返中添加人声效果 audio filter。如果你实现了美声、音效等功能，用户可以在耳返中听到添加效果后的声音
        //agoraKit.enable(inEarMonitoring: true, includeAudioFilters: .builtInAudioFilters)
        
        //在耳返中添加降噪 audio filter
        //agoraKit.enable(inEarMonitoring: true, includeAudioFilters: .noiseSuppression)
        
        
        if intAudioFilters == 0 {
            agoraKit.enable(inEarMonitoring: false)
        }
        else if (intAudioFilters == 1) {
            agoraKit.enable(inEarMonitoring: true)
        }
        else if (intAudioFilters == 2) {
            agoraKit.enable(inEarMonitoring: true, includeAudioFilters: .none)
        }
        else if (intAudioFilters == 3) {
            agoraKit.enable(inEarMonitoring: true, includeAudioFilters: .builtInAudioFilters)
        }
        else if (intAudioFilters == 4) {
            agoraKit.enable(inEarMonitoring: true, includeAudioFilters: .noiseSuppression)
        }
        else {
            agoraKit.enable(inEarMonitoring: false)
            intAudioFilters = 0
            btnEarMonitorNull.backgroundColor = UIColor.lightGray
            btnEarMonitorNone.backgroundColor = UIColor.lightGray
            btnbuiltInAudioFilters.backgroundColor = UIColor.lightGray
            btnnoiseSuppression.backgroundColor = UIColor.lightGray
            btnCloseEra.backgroundColor = UIColor.red
        }
        
        //调节音频采集信号音量,取值范围为 [0,400]
        agoraKit?.adjustRecordingSignalVolume(100)
        
        //设置耳返音量
        agoraKit.setInEarMonitoringVolume(80)
        
        
        agoraKit.setDefaultAudioRouteToSpeakerphone(true)
        let option = AgoraRtcChannelMediaOptions()
        option.publishCameraTrack = .of(true)
        option.clientRoleType = .of((Int32)(AgoraClientRole.broadcaster.rawValue))
        

        let result = agoraKit.joinChannel(byToken: "", channelId: channelName, uid: 0, mediaOptions: option)
        if result != 0 {
            NSLog("打印了 进入频道失败 \(result)")
        }
        else {
            NSLog("打印了 进入频道返回0")
        }
    }
    
    func setupLocalVideo() {
        // 启用视频模块
        //agoraKit?.enableVideo()
        let videoCanvas = AgoraRtcVideoCanvas()
        videoCanvas.uid = 0
        videoCanvas.renderMode = .hidden
        videoCanvas.view = localView
        // 设置本地视图
        agoraKit?.setupLocalVideo(videoCanvas)
        agoraKit.startPreview()
    }
    
    func setupRemoteVideo(_ uid: UInt) {
        let videoCanvas = AgoraRtcVideoCanvas()
        videoCanvas.uid = uid
        videoCanvas.renderMode = .hidden
        videoCanvas.view = remoteView
        // 设置远端视图
        agoraKit?.setupRemoteVideo(videoCanvas)
    }
    
}




extension ZSNLuoDataViewController: AgoraRtcEngineDelegate {
    
    func rtcEngine(_ engine: AgoraRtcEngineKit, didOccurWarning warningCode: AgoraWarningCode) {
        //NSLog("打印了 didOccurWarning: \(warningCode.rawValue)")
    }
    
    func rtcEngine(_ engine: AgoraRtcEngineKit, didOccurError errorCode: AgoraErrorCode) {
        //NSLog("打印了 didOccurError: \(errorCode) Error \(errorCode.rawValue)")
        //self.showAlert(title: "Error", message: "Error \(errorCode.description) occur")
    }
    
    func rtcEngine(_ engine: AgoraRtcEngineKit, didJoinChannel channel: String, withUid uid: UInt, elapsed: Int) {
        //NSLog("打印了 didJoinChannel \(channel) with uid \(uid) elapsed \(elapsed)ms")
        
        setupLocalVideo()
    }
    
    func rtcEngine(_ engine: AgoraRtcEngineKit, didJoinedOfUid uid: UInt, elapsed: Int) {
        //NSLog("打印了 remote user join: \(uid) \(elapsed)ms")
        
        setupRemoteVideo(uid)
    }
    
    func rtcEngine(_ engine: AgoraRtcEngineKit, didOfflineOfUid uid: UInt, reason: AgoraUserOfflineReason) {
        //NSLog("打印了 remote user left: \(uid) reason \(reason)")
    }
    
    func rtcEngine(_ engine: AgoraRtcEngineKit, audioMixingPositionChanged position: Int) {
         
    }
}

extension ZSNLuoDataViewController: AgoraAudioFrameDelegate {
    func onEarMonitoringAudioFrame(_ frame: AgoraAudioFrame) -> Bool {
        //NSLog("打印了 onEarMonitoringAudioFrame 无channelId")
        
        //let fra: AgoraAudioFrame  = frame
        //fra.buffer?.removeAll()
        
        return true
    }
    
    
    

    func onRecord(_ frame: AgoraAudioFrame, channelId: String) -> Bool {
        //NSLog("打印了 onRecord")
        return true
    }
    
    func onPlaybackAudioFrame(_ frame: AgoraAudioFrame, channelId: String) -> Bool {
        //NSLog("打印了 onPlaybackAudioFrame 1")
        return true
    }
    
    func onMixedAudioFrame(_ frame: AgoraAudioFrame, channelId: String) -> Bool {
        //NSLog("打印了 onMixedAudioFrame")
        return true
    }
    
    func onEarMonitoringAudioFrame(_ frame: AgoraAudioFrame, channelId: String) -> Bool {
        
        //NSLog("打印了 onEarMonitoringAudioFrame")
        
        //let fra: AgoraAudioFrame  = frame
        //fra.buffer?.removeAll()
        
        return true
    }
    
    func onPlaybackAudioFrame(beforeMixing frame: AgoraAudioFrame, channelId: String, uid: UInt) -> Bool {
        //NSLog("打印了 onPlaybackAudioFrame beforeMixing")
        return true
    }
    


}

extension ZSNLuoDataViewController: AgoraVideoFrameDelegate {
    
    func onCapture(_ videoFrame: AgoraOutputVideoFrame) -> Bool {
        //NSLog("打印了 onCapture")
        return true
    }
    
    func onCapture(_ srcFrame: AgoraOutputVideoFrame, dstFrame: AutoreleasingUnsafeMutablePointer<AgoraOutputVideoFrame?>?) -> Bool {
        
        return true
    }
    
    func onRenderVideoFrame(_ videoFrame: AgoraOutputVideoFrame, uid: UInt, channelId: String) -> Bool {
        //NSLog("打印了 onRenderVideoFrame")
        return true
    }
    
    func getVideoFrameProcessMode() -> AgoraVideoFrameProcessMode {
        //NSLog("打印了 getVideoFrameProcessMode return .readOnly")
        return .readWrite
    }
    
    func getVideoPixelFormatPreference() -> AgoraVideoFormat {
        //NSLog("打印了 getVideoPixelFormatPreference return .cvPixel")
        return .cvPixel
    }
    
    func getMirrorApplied() -> Bool {
        //NSLog("打印了 getMirrorApplied")
        return true
    }
    
    func getObservedFramePosition() -> UInt32 {
        
        //NSLog("打印了 getObservedFramePosition")
        
        return 1
    }
    
}
